<?php
class Item extends AppModel {
	var $name = 'Item';
	var $displayField = 'name';
	var $validate = array(
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
                                'message' => 'Must have a name',
				'allowEmpty' => false,
			),
		),
		'price' => array(
			'notempty' => array(
				'rule' => array('notempty'),
                                'message' => 'Must have a price',
				'allowEmpty' => false,
			),
			'money' => array(
				'rule' => array('money'),
				'message' => 'Price format incorrect',
			),
		),
		'weight' => array(
			'notempty' => array(
				'rule' => array('notempty'),
                                'message' => 'Must have a weight',
				'allowEmpty' => false,
			),
		),
                'image_filename' => array(
                        'Empty' => array(
                                //'check' => false,
                                'message' => "Must have Filename"
                        ),
                        'InvalidExt' => array(
                                'message' => 'This file extension isn\'t allowed.'
                        )
                )
        );

	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Type' => array(
			'className' => 'Type',
			'foreignKey' => 'type_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasAndBelongsToMany = array(
		'Order' => array(
			'className' => 'Order',
			'joinTable' => 'orders_items',
			'foreignKey' => 'item_id',
			'associationForeignKey' => 'order_id',
			'unique' => true,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
			'deleteQuery' => '',
			'insertQuery' => ''
		)
	);
        var $actsAs = array(
            'MeioUpload' => array(
                'image_filename' => array(
                    'dir' => 'img{DS}uploads{DS}images',
                    'create_directory' => false,
                    'allowed_mime' => array('image/jpeg', 'image/pjpeg', 'image/png'),
                    'allowed_ext' => array('.jpg', '.jpeg', '.png'),
                    'zoomCrop' => true,
                    'thumbsizes' => array(
                    'small'  => array('width'=>65, 'height'=>60),
                    'normal' => array('width' => 400, 'height' => 300),
                 ),
                    'default' => 'default.jpg'
                )
            ),
            'Search.Searchable'
        );

	var $filterArgs = array(
		array('name' => 'name', 'type' => 'query', 'method' => 'filterName'),
                array('name' => 'type_id', 'type' => 'value', 'method' => 'filterType'),

	);

	function filterName($data, $field = null) {
		if (empty($data['name'])) {
			return array();
		}
		$name = '%' . $data['name'] . '%';
		return array(
			'OR'  => array(
				$this->alias . '.name LIKE' => $name,
			));
	}
	function filterType($data, $field = null) {
		if (empty($data['type_id'])) {
			return array();
		}
		$type = '%' . $data['type_id'] . '%';
		return array(
			'OR'  => array(
				$this->alias . '.type_id LIKE' => $type,
			));
	}

}
?>